package ElectricCraft.model;

import java.awt.*;

/**
 *  Items orientation list.
 */
public final class Orientation {
	public static final int UNKNOWN         = -1;

	public static final int NORTH           = 0;
	public static final int EAST            = 1;
	public static final int SOUTH           = 2;
	public static final int WEST            = 3;

	public static final int NORTH_SOUTH = 0;
	public static final int WEST_EAST = 1;

	public static final int NORTH_MASK      = 0x0001;
	public static final int EAST_MASK       = 0x0002;
	public static final int SOUTH_MASK      = 0x0004;
	public static final int WEST_MASK       = 0x0008;

    public static final int NORTH_SOUTH_MASK = NORTH_MASK | SOUTH_MASK;
    public static final int WEST_EAST_MASK  = WEST_MASK | EAST_MASK;

    public static final int EVERYWHERE_MASK = 0x000f;

	public static final int MASK[]          = new int[] {
		0x0001,
		0x0002,
		0x0004,
		0x0008,
	};

	public static final int [] DX           = new int[] { 0, 1, 0, -1 };
	public static final int [] DY           = new int[] { -1, 0, 1, 0 };
    public static final int [] DXY_DIRS     = new int[] { NORTH, EAST, SOUTH, WEST };

    public static final int [] DX_WE        = new int[] { -1, 1 };
    public static final int [] DY_WE        = new int[] {  0, 0 };
    public static final int [] DXY_WE_DIRS  = new int[] { WEST, EAST };

    public static final int [] DX_NS        = new int[] {  0, 0 };
    public static final int [] DY_NS        = new int[] { -1, 1 };
    public static final int [] DXY_NS_DIRS  = new int[] { NORTH, SOUTH };

	public static int invertDir(int dir) {
		return (dir + 2) % 4;
	}

	public static int rotateDir(int dir, boolean clockwise) {
		if (clockwise) {
            return (dir + 1) % 4;
        } else {
            return (dir + 3) % 4;
        }
	}

	public static int getDirByNeighbours(Point prev, Point cur) {
		int dx = cur.x - prev.x;
		int dy = cur.y - prev.y;

		if (dx != 0 && dy != 0 || Math.abs(dx + dy) != 1) {
			return UNKNOWN;
		}

		if (dx == 1) {
			return EAST;
		}
		if (dx == -1) {
			return WEST;
		}
		if (dy == 1) {
			return SOUTH;
		}

		return NORTH;
	}

    public static boolean checkDir(int wireType, int dir) {
        return (wireType & MASK[dir]) != 0;
    }

    public static boolean checkMask(int wireType, int mask) {
        return (wireType & mask) != 0;
    }

    public static int convertToTwoSidedDir(int dir) {
        return dir & 1;
    }
}
